﻿package 
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import graphics.pixelbender.src.*;
	import samples.BaseSample;
	import samples.d3.color.*;
	import samples.d3.image.*;
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Images extends Sprite 
	{
		
		private var sampleId:int = 0;
		private var tf:TextFormat;
		private var txt:TextField;
		
		private var sample:BaseSample;
		
		public function Images():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			_stage = stage;
			_stage.align = StageAlign.TOP_LEFT;
			_stage.scaleMode = StageScaleMode.NO_SCALE;
			_stage.displayState = StageDisplayState.FULL_SCREEN;
			
			_stage.addEventListener( KeyboardEvent.KEY_DOWN, keyHandler )
			
			txt = new TextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.selectable = false;
			txt.antiAliasType = AntiAliasType.ADVANCED;
			addChild( txt );
			
			tf = new TextFormat( 'Verdana', 16, 0xFFFFFF );
			sampleId = 0;
			keyHandler();
		}
		
		private function keyHandler(e:KeyboardEvent = null ):void 
		{
			if ( 	e != null
			&&		e.keyCode != Keyboard.RIGHT 
			&&		e.keyCode != Keyboard.LEFT 
			&&		e.keyCode != Keyboard.UP ) return;
			
			if ( e != null )
			{
				switch( e.keyCode )
				{
					case Keyboard.RIGHT:
					sampleId++;
					break;
					
					case Keyboard.LEFT:
					sampleId--;
					break;
					
				}
			}
			
			if ( sampleId < 0) sampleId = 0;
			
			var n:int = numChildren;
			while ( n-- )
			{
				if( !( getChildAt( n ) is TextField ) ) removeChildAt( n );
			}
			
			var description:String = '';
			var op:int = 0;
			switch( sampleId )
			{
				case op++:
				description = 'a color is the additiion of 3 to 4 channels';
				sample = new Color0();
				break;
				
				case op++:
				description = 'hue';
				sample = new Color1();
				break;
				
				case op++:
				description = 'saturation';
				sample = new Color2();
				break;
				
				
				case op++:
				description = 'this question is too vast to be adressed here.\nit is very easy to create ugliness though...';
				sample = new Color4();
				break;
				
				
				case op++:
				description = 'an image is a basically a 2d matrix of (A)RGB colors squares\naka picture elements\naka pixels';
				sample = new Sampling();
				break;
				
				case op++:
				description = '(A)RGB matrices are stored separately as individual geyscale matrices\nthe values of each pixels range from 0 to 255 inclusive and is called a channel';
				sample = new Layers();
				break;
				
				case op++:
				description = 'histogram: each channel can be quantified as an histogram\nen histogram is a 2D curve that represents the quantity of each greyscale value for each channel\nin Flash, we can\'t set the histogram, just get it ';
				
				trace( 'histogram' );
				sample = new Histogram();
				break;
				
				
				case op++:
				description = 'we can remap the palette to some values';
				sample = new PaletteMap();
				break;
				
				case op++:
				description = 'we can also select ranges of colors with a threshold';
				sample = new Threshold();
				break;
				
				
				case op++:
				description = 'a quick and dirty trick to get the outlines: a glowfilter :)';
				sample = new Outline();
				break;
				
				case op++:
				description = 'the component labeling: identifies the color regions of a picture';
				sample = new ComponentLabellingSample();
				break;
				
				case op++:
				description = 'the colors of the pixels can be thought of as 3D vectors';
				sample = new ColorSpace();
				break;
				
				
				case op++:
				description = 'some pixelBender stuff: bulge';
				sample = new Bulge();
				break;
				
				case op++:
				description = 'some pixelBender stuff: jitter';
				sample = new Jitter();
				break;
				
				case op++:
				description = 'some pixelBender stuff: slices';
				sample = new Slices();
				break;
				
				case op++:
				description = 'some pixelBender stuff: waves';
				sample = new Wave();
				break;
				
				case op++:
				description = 'some pixelBender stuff: bi unit square\nnormalize image size between -1 | 1\ndo all kind of fancy tweeaks\nremap to the original image size';
				sample = new Normalized();
				break;
				
				//case op++:
				//description = 'some pixelBender stuff: flame IFS';
				//sample = new Flame();
				//break;
				
			}
			
			txt.text = sampleId.toString() +' : ' + description;
			txt.setTextFormat( tf );
			if( sample != null ) addChild( sample );
			
		}
		
	}
	
}